//
//  HMComposePictureView.swift
//  WB25
//
//  Created by HM on 2017/7/8.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
// cell可重用标识符
private let HMComposePictureViewCellID = "HMComposePictureViewCellID"

class HMComposePictureView: UICollectionView {
    
    
    // 01-声明闭包
    var closure:(()->())?
    
    // 实例化一个数组(类型 UIimage)
    lazy var imageList: [UIImage] = [UIImage]()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let cellMargin: CGFloat = 5
        let cellWH = (HMSCREENW - 20 - cellMargin*2)/3
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: cellWH, height: cellWH)
        // 间距
        flowLayout.minimumLineSpacing = cellMargin
        flowLayout.minimumInteritemSpacing = cellMargin
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 提供外界调用的方法
    func addImage(image: UIImage){
        // 添加图片到数组中
        imageList.append(image)
        // 显示配图
        if imageList.count == 1 {
            isHidden = false
        }
        // 刷新
        reloadData()
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        // 首次创建隐藏
        isHidden = true
        backgroundColor = UIColor.white
        dataSource = self
        delegate = self
        register(HMComposePictureViewCell.self, forCellWithReuseIdentifier: HMComposePictureViewCellID)
    }

}
//MARK: - UICollectionViewDataSource
extension HMComposePictureView: UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // 计算下imageList的count
        let count = imageList.count
        // 如果count == 0 或者 count == 9 count为多少 就有多少个item
        // 反之 count + 1
        if count == 0 || count == 9 {
            return count
        }
        return count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HMComposePictureViewCellID, for: indexPath) as! HMComposePictureViewCell
        // 赋值 
        
        /*
            - 假设 imageList = [图片, 图片] count 2 item (0,1)
            - 如果是2个 (item cell 个数 3) item (0,1,2)
         */
        
        // 如果count == item (代表有加号按钮)
        if imageList.count == indexPath.item {
            cell.image =  nil
            
        }else{
            cell.image =  imageList[indexPath.item]
            
            // 实例化闭包 和闭包回调
            cell.closure = {[weak self] in
                self?.imageList.remove(at: indexPath.item)
                // 如果imageList等于0 就要隐藏
                self?.isHidden = (self?.imageList.count == 0)
                self?.reloadData()
            }
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        // 只有点击加号图片才打开相册
        if imageList.count == indexPath.item {
            // 03-执行闭包
            closure?()
        }
    }
}
